package com.example.books;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.books.data.Book;
import com.example.books.data.Collection;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class CollectionActivity extends AppCompatActivity {

    private ListView listCollection;
    private List<String> stringList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        add();
        initView();

    }

    private void add() {
        SharedPreferences sp = getSharedPreferences("id", MainActivity.MODE_PRIVATE);
        int id = sp.getInt("sid", 0);
        List<Collection> collections = LitePal.where("userId=?",""+id).find(Collection.class);
        long[] ids = new long[collections.size()];
        int i=0;
        for (Collection collection:collections) {
            long bookId = collection.getBookId();
            ids[i] = bookId;
            i++;
        }
        List<Book> all = LitePal.findAll(Book.class, ids);
        for (Book b : all) {
            String boolkName = b.getBoolkName();
            stringList.add(boolkName+id+"iiii"+ids.length+"ddd"+collections.size());
        }
    }

    private void initView() {
        listCollection = (ListView) findViewById(R.id.listCollection);
        ArrayAdapter adapter = new ArrayAdapter(CollectionActivity.this,android.R.layout.simple_list_item_1,stringList);
        listCollection.setAdapter(adapter);
    }
}
